package com.s3u4.demo.zookeeper.base;

import com.s3u4.demo.zookeeper.ZkConfig;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;

import java.io.IOException;

/**
 * 加入组
 * Created by Captain on 01/08/2017.
 */
public class JoinWatcher extends ConnectionWatcher {

    public void join(String groupName,String memberName) throws KeeperException, InterruptedException {
        String path = "/" + groupName + "/" + memberName;
        // 关闭连接后znode被自动删除
        String createPath = zooKeeper.create(path,null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("Created : " + createPath);
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        JoinWatcher watcher = new JoinWatcher();
        watcher.connect(ZkConfig.ZK_HOST);
        watcher.join("demo-zookeeper","node1");
        // stay alive until process is killed or thread is interrupted
        Thread.sleep(Long.MAX_VALUE);
    }

}
